package adventure.commands;

import adventure.*;
import player.Player;
import colour.ColourUtil;

/**
 *
 * @author Michael Hanns
 *
 */
public class Look extends AbstractCommand {

    public Look(Adventure a) {
        super(a, new String[] {"LOOK"});
    }

    @Override
    public String validateAndExecute(Player player, String[] args) {
        if(args.length == 1) {
            return adv.printRoom(player);
        } else {
            return look(player, Util.consolidateArguments(args));
        }
    }

     private String look(Player player, String object) {
        int x = player.x();
        int y = player.y();
        int z = player.z();


        String itemDesc[] = player.getItemDescription(object);

        if (itemDesc != null) {
            out.broadcastToPlayer(player,
                    "You look at " 
                    + ColourUtil.colourise(itemDesc[0], ColourUtil.cyan) + ".");
            
            return itemDesc[1];
        } else {
            String output = rooms.getPlaceableDescription(x, y, z, object);

            // If it's a player, broadcast the action
            int targetID = rooms.getRoomPlayer(x, y, z, object);
            if (targetID > 0) {
                Player[] localPlayers = new Player[2];
                localPlayers[0] = player;
                localPlayers[1] = players.getOnlinePlayer(targetID);

                if (targetID == player.getID()) {
                    out.broadcastToPlayer(localPlayers[1],
                            "You look at yourself.");
                } else {
                    out.broadcastLocalToPlayers(localPlayers,
                            ColourUtil.colourise(player.getName(), ColourUtil.magenta)
                            + " looks at "
                            + ColourUtil.colourise(localPlayers[1].getName(), ColourUtil.magenta)
                            + ".");
                    out.broadcastToPlayer(localPlayers[1],
                            ColourUtil.colourise(player.getName(), ColourUtil.magenta)
                            + " looks at you.");
                }
            }

            return output;
        }
    }
}
